from 第18天.interface.wechat_request import WechatRequest
from 第18天.vo.department import Department


class WechatDepartmentRequest(WechatRequest):
    def get_sub_department_id(self, department_id: int = None):
        params = {"id": department_id} if department_id is not None else {}
        result_json = self.get("/cgi-bin/department/simplelist", params)
        self._assert_result(result_json)
        return [Department(single_department) for single_department in result_json.get('department_id')]

    def create_department(self, department: Department):
        result_json = self.post("/cgi-bin/department/create", {
           "name": department.name,
           "name_en": department.name_en,
           "parentid": department.parent_id,
           "order": department.order,
           "id": department.id
        })
        self._assert_create_result(result_json)
        return result_json.get("id")


if __name__ == "__main__":
    wechat_department_request = WechatDepartmentRequest()
    # result = wechat_department_request.create_department(
    #     Department(
    #         {
    #             "name": "上海研发中心",
    #             "name_en": "RDSH",
    #             "parentid": 1,
    #             "order": 1,
    #             "id": 4
    #         }
    #     )
    # )
    result = wechat_department_request.get_sub_department_id()
    print(result)
